Imports System.Data.SqlClient

Public Class ObraSocial : Implements IBusquedaFiltrado
    Private IdObraSocial As Integer
    Private Descrip As String
    'Private IdPlan As Plan
    Public Property IdOS() As Integer
        Get
            Return IdObraSocial
        End Get
        Set(ByVal value As Integer)
            IdObraSocial = value
        End Set
    End Property
    Public Property DescripOS() As String
        Get
            Return Descrip
        End Get
        Set(ByVal value As String)
            Descrip = value
        End Set
    End Property
    'Public Property IdPlanOS() As Plan
    '    Get
    '        Return IdPlan
    '    End Get
    '    Set(ByVal value As Plan)
    '        IdPlan = value
    '    End Set
    'End Property

    Public Sub New()
        IdObraSocial = 0
        Descrip = ""
        'IdPlan = New Plan
    End Sub

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlDataAdapter
        Dim rs As DataTableReader
        Dim dato As DataTable
        Dim sql As String
        Dim lResp As Boolean = False

        

        sql = String.Format("SELECT * FROM ObraSocial WHERE IdObraSocial ={0}", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("obrasocial")
        comando.Fill(dato)
        rs = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()

        If rs.Read Then
            IdObraSocial = System.Convert.ToInt16(rs("IdObraSocial"))
            Descrip = rs("Descrip").ToString
            'IdPlan = New Plan()
            'IdPlan.Buscar(rs("IdPlan"), con)
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function

    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO ObraSocial (IdObraSocial, Descrip)" + _
              "  VALUES({0},'{1}')"
        sql = String.Format(sql, IdObraSocial, Descrip)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE ObraSocial " + _
              "  SET Descrip = '{0}'," + _
              "  WHERE IdObraSocial = {3}"
        sql = String.Format(sql, Descrip, IdObraSocial)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
            Implements IBusquedaFiltrado.Eliminar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "DELETE FROM ObraSocial WHERE IdObraSocial ={0}"
        sql = String.Format(sql, id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT distinct descrip , idObraSocial FROM OBRASOCIAL"
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "obrasocial")

        Return datos
    End Function
    Public Function BuscarId(ByVal DescOS As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM OBRASOCIAL WHERE Descrip='{0}'", DescOS)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            IdObraSocial = System.Convert.ToInt16(rs("idobrasocial"))
            Descrip = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
End Class
